import {_decorator, Component, director, instantiate, Node, Prefab, resources} from 'cc';
import {userInfo} from '../../user-info';
import {GameEvent} from '../../game-event';
import {FishComp} from '../fish/fish-comp';
import {FreshImmortal, ImmortalDetail} from "db://assets/proto/shared/v1/wuni";
import {Fish} from "db://assets/script/game/entity/fish";

const {ccclass, property} = _decorator;

@ccclass('BottomUI')
export class BottomUI extends Component {
    @property(Node)
    content!: Node

    start() {
        director.on(GameEvent.userFishRemove, this._removeFish, this);
        this.onEvent()
        this.updateUI();
    }

    onEvent() {
        director.on(GameEvent.userFishAdd, this._addFish, this)
    }

    offEvent() {
        director.off(GameEvent.userFishAdd, this._addFish, this)
    }

    //
    private _addFish(fish: ImmortalDetail) {
        const fishNode = instantiate(resources.get("prefab/fish", Prefab)!)
        this.content.addChild(fishNode)
        fishNode.getComponent(FishComp)!.setFish(fish.fish!.id!)
        // this.createBtn.active = userInfo.loadFishs().size < 6;
    }

    private _removeFish(ff: FreshImmortal) {
        // this.createBtn.active = userInfo.loadFishs().size < 6;
    }


    updateUI() {
        const fishs = userInfo.loadFishs()
        fishs.forEach((fish: Fish) => {
            const fishNode = instantiate(resources.get("prefab/fish", Prefab)!)
            this.content.addChild(fishNode)
            fishNode.getComponent(FishComp)!.setFish(fish.freshFish!.id!)
        })
    }

    update(deltaTime: number) {

    }

    protected onDestroy() {
        this.offEvent()
    }
}


